﻿using System;
using System.Web.UI.WebControls;
using SystemX.Infrastructure;

namespace SystemX.WebControls
{
	public class JQTheme:WebControl
	{
		/// <summary>
		/// Jquery theme name
		/// </summary>
		public string Name { get; set; }
		private static readonly object _SynHelper = new object();
		private static string _Html;
		public string OuterHtml
		{
			get
			{
				if (_Html == null)
				{
					lock (_SynHelper)
					{
						if (_Html == null)
						{
							_Html = LoadAll();
						}
					}
				}
				return _Html;
			}
		}

		protected override void Render(System.Web.UI.HtmlTextWriter writer)
		{
			writer.Write(OuterHtml);
		}

		private string LoadAll()
		{
			Name = Name ?? "ui-lightness";
			var cssUrl = AppSettingsHelper.Instance.GetString("JQuery.CssUrl");
			cssUrl = string.Format(cssUrl, Name);

			using (var cssObj=new SiteCSS())
			{
				cssObj.CSSRelativeToRoot = cssUrl;
				return cssObj.Script;
			}
		}
	}
}
